package com.feng.webflux.service;

import com.feng.demo.entity.User;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    //创建list集合存储数据
    private final List<User>  users = new ArrayList<>();
    public UserServiceImpl(){
        this.users.add(new User(1,"Luky","女",18));
        this.users.add(new User(2,"Mary","女",17));
        this.users.add(new User(3,"Kangkang","男",18));
    }

    @Override
    public Mono<User> getUserById(int id) {
        return Mono.just(users.get(id));
    }

    @Override
    public Flux<User> getAllUser() {
        return Flux.fromIterable(this.users);
    }

    @Override
    public Mono saveUserInfo(Mono<User> user) {
        return user.doOnNext(person ->{
            users.add(person);
        }).thenEmpty(Mono.empty());
    }
}
